<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once '/application/libraries/PHPExcel.php';

class Agency extends CI_Controller {

    //构造函数
    public function __construct()
    {
		parent::__construct();
		$this->load->model('Config_m');
	    $this->load->model('Agent_m');
	}

	/*转赠日志-天数*/
	public function zzdate_log(){
			if(isset($_POST['key'])){
			    $key = trim($_POST['key']);
				$wheres = "and my_device_sn like '%{$key}%'";
			    $this->db->like('my_device_sn', $key);
			}else{
				$wheres = "";
			}
			$this->db->where('types', '1');
			$total = $this->db->count_all_results('zhuanzheng_log');
			$page = $_POST['page'];
	        $limit = $_POST['limit'];
	        $offset = ($page - 1) * $limit;
	        $logsql = "select * from  mifi_zhuanzheng_log where types = 1 $wheres order by log_id desc limit ".$offset.",".$limit."";
	        $datas = $this->db->query($logsql)->result_array();
	        foreach ($datas as $key => $val) {
	        	$datas[$key]['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
	        }
		    echo json_encode(['code'=>0,'count'=>$total,'data'=>$datas]);
	}

	/*转赠日志-天数*/
	public function zzliuliang_log(){
			if(isset($_POST['key'])){
			    $key = trim($_POST['key']);
				$wheres = "and my_device_sn like '%{$key}%'";
			    $this->db->like('my_device_sn', $key);
			}else{
				$wheres = "";
			}
			$this->db->where('types', '2');
			$total = $this->db->count_all_results('zhuanzheng_log');
			$page = $_POST['page'];
	        $limit = $_POST['limit'];
	        $offset = ($page - 1) * $limit;
	        $logsql = "select * from  mifi_zhuanzheng_log where types = 2 $wheres order by log_id desc limit ".$offset.",".$limit."";
	        $datas = $this->db->query($logsql)->result_array();
	        foreach ($datas as $key => $val) {
	        	$datas[$key]['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
	        }
		    echo json_encode(['code'=>0,'count'=>$total,'data'=>$datas]);
	}

	/*代理列表*/
	public function onedailiList()
	{
		
			if(isset($_POST['key'])){
			    $key = trim($_POST['key']);
				$agent_name = "and agent_name like '%{$key}%'";
			    $this->db->like('agent_name', $key);
			}else{
				$agent_name = "";
			}
			$this->db->where('agent_level', '1');
			$total = $this->db->count_all_results('agent');
			$page = $_POST['page'];
	        $limit = $_POST['limit'];
	        $offset = ($page - 1) * $limit;
	        $sql = "select * from  mifi_agent where agent_level = 1 $agent_name order by id desc limit ".$offset.",".$limit."";
	        $data = $this->db->query($sql)->result_array();
	        foreach ($data as $key => $val) {
	        	if ($val['sex'] == 0) {
	        		$data[$key]['sex'] = '女';
	        	}else{
	        		$data[$key]['sex'] = '男';
	        	}
	        }
		    echo json_encode(['code'=>0,'count'=>$total,'data'=>$data]);

			
	}

	public function twodailiList()
	{
		if(isset($_POST['key'])){
			    $key = trim($_POST ['key']);
				$agent_name = "and agent_name like '%{$key}%'";
				$this->db->like('agent_name', $key);
			}else{
				$agent_name = "";
			}
			$this->db->where('agent_level', '2');
			$total = $this->db->count_all_results('agent');
			$page = $_POST['page'];
	        $limit = $_POST['limit'];
	        $offset = ($page - 1) * $limit;
	        $sql = "select * from  mifi_agent where agent_level = 2 $agent_name order by id desc limit ".$offset.",".$limit."";
	        $data = $this->db->query($sql)->result_array();
	        foreach ($data as $key => $val) {
	        	if ($val['sex'] == 0) {
	        		$data[$key]['sex'] = '女';
	        	}else{
	        		$data[$key]['sex'] = '男';
	        	}
	        }
		    echo json_encode(['code'=>0,'count'=>$total,'data'=>$data]);
	}
	public function sandailiList()
	{
		
			if(isset($_POST['key'])){
			    $key = trim($_POST['key']);
				$agent_name = "and agent_name like '%{$key}%'";
			    $this->db->like('agent_name', $key);
			}else{
				$agent_name = "";
			}
			$this->db->where('agent_level', '3');
			$total = $this->db->count_all_results('agent');
			$page = $_POST['page'];
	        $limit = $_POST['limit'];
	        $offset = ($page - 1) * $limit;
	        $sql = "select * from  mifi_agent where agent_level = 3 $agent_name order by id desc limit ".$offset.",".$limit."";
	        $data = $this->db->query($sql)->result_array();
	        foreach ($data as $key => $val) {
	        	if ($val['sex'] == 0) {
	        		$data[$key]['sex'] = '女';
	        	}else{
	        		$data[$key]['sex'] = '男';
	        	}
	        }
		    echo json_encode(['code'=>0,'count'=>$total,'data'=>$data]);

			
	}
	public function sidailiList()
	{
		
			if(isset($_POST['key'])){
			    $key = trim($_POST['key']);
				$agent_name = "and agent_name like '%{$key}%'";
			    $this->db->like('agent_name', $key);
			}else{
				$agent_name = "";
			}
			$this->db->where('agent_level', '4');
			$total = $this->db->count_all_results('agent');
			$page = $_POST['page'];
	        $limit = $_POST['limit'];
	        $offset = ($page - 1) * $limit;
	        $sql = "select * from  mifi_agent where agent_level = 4 $agent_name order by id desc limit ".$offset.",".$limit."";
	        $data = $this->db->query($sql)->result_array();
	        foreach ($data as $key => $val) {
	        	if ($val['sex'] == 0) {
	        		$data[$key]['sex'] = '女';
	        	}else{
	        		$data[$key]['sex'] = '男';
	        	}
	        }
		    echo json_encode(['code'=>0,'count'=>$total,'data'=>$data]);

			
	}
	/*代理设备详情-*/
	public function daili_details()
	{
		if(isset($_POST['key'])){
				$key = trim($_POST['key']);
				$daili_id = $_POST['daili_id'];
				$device_sn = "where b.id= '".$daili_id."' and device_sn like '%{$key}%'";
		        $limit = $_POST['limit'];
			}else{
				$daili_id = $_POST['daili_id'];
				$device_sn = "where b.id= '".$daili_id."'";
		        $limit = $_POST['limit'];
			}
			$dai['id'] = $daili_id;
			$level = $this->Agent_m->get_one($dai);

			if ($level['agent_level'] == 1) {
				if(isset($_POST['key'])){
					$key = trim($_POST['key']);
					$this->db->like('device_sn',$key);
				}
				$this->db->where('agent1', $daili_id);
				$total = $this->db->count_all_results('device');
				$page = $_POST['page'];
		        $offset = ($page - 1) * $limit;
		        $sql = "select a.*,b.agent_name from  mifi_device as a left join mifi_agent as b on b.id = a.agent1 $device_sn order by id desc limit ".$offset.",".$limit."";
		        $data = $this->db->query($sql)->result_array();
		        foreach ($data as $key => $val) {
		        	$data[$key]['dengji'] = '一级代理';
		        }
			}

			if ($level['agent_level'] == 2) {
				if(isset($_POST['key'])){
					$key = trim($_POST['key']);
					$this->db->like('device_sn',$key);
				}
				$this->db->where('agent2', $daili_id);
				$total = $this->db->count_all_results('device');
				$page = $_POST['page'];
		        $offset = ($page - 1) * $limit;
		        $sql = "select a.*,b.agent_name from  mifi_device as a left join mifi_agent as b on b.id = a.agent2 $device_sn order by id desc limit ".$offset.",".$limit."";
		        $data = $this->db->query($sql)->result_array();
		        foreach ($data as $key => $val) {
		        	$data[$key]['dengji'] = '二级代理';
		        }
			}

			if ($level['agent_level'] == 3) {
				if(isset($_POST['key'])){
					$key = trim($_POST['key']);
					$this->db->like('device_sn',$key);
				}
				$this->db->where('agent3', $daili_id);
				$total = $this->db->count_all_results('device');
				$page = $_POST['page'];
		        $offset = ($page - 1) * $limit;
		        $sql = "select a.*,b.agent_name from  mifi_device as a left join mifi_agent as b on b.id = a.agent3 $device_sn order by id desc limit ".$offset.",".$limit."";
		        $data = $this->db->query($sql)->result_array();
		        foreach ($data as $key => $val) {
		        	$data[$key]['dengji'] = '三级代理';
		        }
			}

			if ($level['agent_level'] == 4) {
				if(isset($_POST['key'])){
					$key = trim($_POST['key']);
					$this->db->like('device_sn',$key);
				}
				$this->db->where('agent4', $daili_id);
				$total = $this->db->count_all_results('device');
				$page = $_POST['page'];
		        $offset = ($page - 1) * $limit;
		        $sql = "select a.*,b.agent_name from  mifi_device as a left join mifi_agent as b on b.id = a.agent4 $device_sn order by id desc limit ".$offset.",".$limit."";
		        $data = $this->db->query($sql)->result_array();
		        foreach ($data as $key => $val) {
		        	$data[$key]['dengji'] = '四级代理';
		        }
			}
		    echo json_encode(['code'=>0,'count'=>$total,'data'=>$data,'num'=>$total]);

	}

	/*代理订单详情*/
	public function daili_order_list(){
		if(isset($_POST['key'])){
			$key = $_POST['key'];
				$device_sn = " and device_sn like '%{$key}%'";
			}else{
				$device_sn = " ";
			}
			
			$getid = $_POST['id'];
		    $limit = $_POST['limit'];
			$total = $this->db->count_all_results('commission');
			$page = $_POST['page'];
	        $offset = ($page - 1) * $limit;
	        $sql = "select a.*,b.order_on,c.agent_name from  mifi_commission as a left join mifi_order as b on b.orderid = a.order_id left join mifi_agent as c on a.agent_user_id = c.id where settlement=1 and agent_user_id = $getid $device_sn order by id desc limit ".$offset.",".$limit."";
	        $data = $this->db->query($sql)->result_array();
	        //统计
	        $sqls = "SELECT SUM(`money`) as num FROM mifi_commission where agent_user_id=$getid and settlement=1";
	        $num = $this->db->query($sqls)->result_array();
	        if (empty($num[0]['num'])) {
	        	$num[0]['num'] = 0;
	        }
	        foreach ($data as $key => $va) {
	        	if ($va['settlement'] == 0) {
	        		$data[$key]['settlement'] = '未结算';
	        	}else{
	        		$data[$key]['settlement'] = '已结算';
	        	}
	        }
		    echo json_encode(['code'=>0,'count'=>$total,'data'=>$data,'num'=>$num[0]['num']]);
	}
    
    //添加代理
    public function agentadds()
	{
		$agent = $_POST;
		//判断数据库是否存在QQ账号
		$sql = "select * from mifi_agent where agent_qq = '{$agent['agent_qq']}'";	
        $row = $this->db->query($sql)->result_array();
        if($row){
        	echo json_encode(['code'=>0,'infomsg'=>'该QQ账号已经存在，请重新输入']);
        	exit;
        }
		$agent['agent_level'] = 1;
		$agent['leader'] = 0;
		$agent['addtime'] = date('Y-m-d H:i:s');
		$str = $agent['mobile'];
		$agent['password'] = substr($str,5,6);
		$res = $this->db->insert('agent', $agent);
		if($res){
			echo json_encode(['code'=>1,'infomsg'=>'添加成功']);
		}else{
			echo json_encode(['code'=>0,'infomsg'=>'添加失败']);
		}
	}
	// public function agentadds()
	// {
	// 	$agent = $_POST;
	// 	//判断数据库是否存在QQ账号
	// 	$sql = "select * from mifi_agent where agent_qq = '{$agent['agent_qq']}'";	
 //        $row = $this->db->query($sql)->result_array();
 //        if($row){
 //        	echo json_encode(['code'=>0,'infomsg'=>'该QQ账号已经存在，请重新输入']);
 //        	exit;
 //        }

	// 	if($agent['agent_level'] == "一级代理"){
	// 		$agent['agent_level'] = 1;
	// 		$agent['leader'] = 0;
	// 	}else{
	//         $sqls = "select scale from mifi_agent where id = '{$agent['agent_level']}'";	
	//         $rows = $this->db->query($sqls)->row_array();
	//         if($agent['scale'] >= $rows['scale']){
	//         	echo json_encode(['code'=>0,'infomsg'=>'佣金比例不能大于或等于上级代理']);
	//         	exit;
	//         }
	// 		$agent['leader'] = $agent['agent_level'];
	// 		$agent['agent_level'] = 2;
	// 	}
	// 	$agent['addtime'] = date('Y-m-d H:i:s');
	// 	$str = $agent['mobile'];
	// 	$agent['password'] = substr($str,5,6);
	// 	$res = $this->db->insert('agent', $agent); 
	// 	if($res){
	// 		echo json_encode(['code'=>1,'infomsg'=>'添加成功']);
	// 	}else{
	// 		echo json_encode(['code'=>0,'infomsg'=>'添加失败']);
	// 	}
	// }
  
}
